<?php
namespace Index\Controller;
use Think\Controller;
class IndexController extends Controller {

    public $cid = 0;
    public $top_cid = 0 ; //顶层栏目
    public $columns = null;
    public $modelCol = null;

    protected function _initialize(){
        $this->modelCol = M("columns");
        //pid=0 and 
        $list = $this->modelCol->where("status=1")->order("sort asc")->select();
        $arrCols = [];
        foreach ($list as $key => $value) {
            $arrCols[$value['id']] = $value;
        }
        foreach ($list as $key => $value) {
            if($value['pid']){
                $arrCols[$value['pid']]['child'][] = $value;
            }
            if($value['module']=='link'){
                $arrCols[$value['id']]['link'] = $value['url'];
            }else{

                $arrCols[$value['id']]['link'] = '?s=/index/index/' . 
                    $value['module'] . '&cid=' . $value['id'];
            }
            
        }
        //dd($arrCols);
        $this->columns = $arrCols;
        $cid = I("cid",0);
        $id = I("id",0);
        if($cid ){
            if(!$id){
                $this->assign("info", $arrCols[$cid]);
            }
            $this->assign("cat", $arrCols[$cid]);

            $this->top_cid = $arrCols[$cid]['pid'] ? $arrCols[$cid]['pid'] : $cid;
            $this->assign("top_cid", $this->top_cid);
        }
        
        $this->assign("columns", $arrCols);
    }
    
    /* public function __construct()
    {
    } */

    public function index(){
        
        $info = $this->columns[1];
        $this->assign('cat',$info);
        $this->assign('info',$info);
        $this->assign("top_cid", 1);

        //$prodList = m("product")->limit("")

        $prodlist = $this->getProdList();
        //dd($prodlist);
        $this->assign("prodlist", $prodlist);
        //dd($this->columns);
        //dd($prodlist);
        //$artCats = $this->columns[10]['child'];
        //dd($artCats);
        $artlist1 = $this->getArtList(['catid'=>5]);
        $this->assign("artlist1", $artlist1);
        $artlist2 = $this->getArtList(['catid'=>6]);
        $this->assign("artlist2", $artlist2);
        //dd($artlist1);

        $this->display();
    }

    public function articleList()
    {
        $cid = $this->cid;
        $cat = $this->columns[$cid];
        if(count($cats['child'])){

        }

        $this->assign('info',$cat);
    }



    public function article()
    {
        $id = I("id");
        $m = M("article");
        $info= $m->find($id);
        $this->assign("info",$info);
        $this->display();
    }

    private function getArt($cid, $size)
    {
        $m = M("article");
        $list = $m->field("id,title")->where("catid=$cid")->limit("1, $size")->select();
        return $list;
    }

    public function single()
    {
        $id = I("cid");
        $m = M("single");
        $info= $m->find($id);
        $this->assign("info",$info);
        //dd($info);
        $this->display();
    }

    public function productList()
    {
        $cid = I("cid");
    }

    public function product()
    {
        //dd($id);
        $id = I("id");
        $m = M("product");
        $info= $m->find($id);
        $this->assign("info",$info);
        $prodlist = $this->getProdList();
        $this->assign("prodlist", $prodlist);
        $this->display();
    }

    // cid;
    function getProdList($where=[], $size=3)
    {
        $m = M("product");
        //$cid = $where['cid'] ;
        $where['status'] = 1;

        $list = $m->field("id,title,sort, thumb,catid")->where($where)
            ->limit("0, $size")->order("sort asc")->select();

        foreach ($list as $key => $v) {
            $list[$key]['url'] = U('product') ."&cid=".$v['catid']."&id=" . $v['id'] ;
            $list[$key]['thumburl'] = './Uploads/' . $v['thumb'] ;
        }
        return $list;
    }

    function getArtList($where=[], $size=5)
    {
        $m = M("article");
        //$cid = $where['cid'] ;
        $where['status'] = 1;

        $list = $m->field("id,title,sort,catid ")->where($where)
            ->limit("0, $size")->order("sort asc")->select();
            
        foreach ($list as $key => $v) {
            $list[$key]['url'] = U('article') . "&cid=".$v['catid']."&id=" . $v['id'] ;
            //$list[$key]['thumburl'] = './Uploads/' + $v['thumb'] ;
        }
        return $list;
    }


    /* public function mobileIndex()
    {
                $info = $this->columns[1];
        $this->assign('cat',$info);
        $this->assign('info',$info);
        $this->assign("top_cid", 1);

        //$prodList = m("product")->limit("")

        $prodlist = $this->getProdList();
        //dd($prodlist);
        $this->assign("prodlist", $prodlist);
        //dd($this->columns);
        //dd($prodlist);
        //$artCats = $this->columns[10]['child'];
        //dd($artCats);
        $artlist1 = $this->getArtList(['catid'=>5]);
        $this->assign("artlist1", $artlist1);
        $artlist2 = $this->getArtList(['catid'=>6]);
        $this->assign("artlist2", $artlist2);
        //dd($artlist1);

        $this->display('mobile/index');
    }
 */
    
}